package com.shop.cereshop.app.dl.logic.controller;

import com.shop.cereshop.app.dl.logic.service.DlShopProductService;
import com.shop.cereshop.app.dl.logic.utils.ResultInfo;
import com.shop.cereshop.app.domain.shop.CereShopCouponRecording;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.app.service.order.CereShopOrderService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.order.CereShopOrder;
import com.shop.cereshop.commons.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: dlshop
 * 描述：
 * @author: 我好帅
 * @create: 2021-06-16 10:57
 **/
@RestController
@RequestMapping("/api")
@CrossOrigin
public class CereShopProductController {
    @Autowired
    private DlShopProductService dlShopProductService;
    @Autowired
    private CereShopOrderService cereShopOrderService;
    @Autowired
    private CereBuyerUserService cereBuyerUserService;
//    @Autowired
//    private SetRedisService setRedisService;

    //按类型获取商品
    @PostMapping("/getCommodityByType")
    public ResultInfo getCommodityByType(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "row", defaultValue = "10") Integer row, Integer classifyId, String content,
                                            HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        return dlShopProductService.getCommodityByType(page, row, classifyId, content, user);
    }

    //获取首页推荐商品
    @PostMapping("/getCommodityByMark")
    public ResultInfo getCommodityByMark(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "row", defaultValue = "10") Integer row) {
        return dlShopProductService.getCommodityByMark(page, row);
    }

    //搜索历史记录
//    @PostMapping("/searchHistory")
//    public ResultInfo searchHistory() {
//        Set<Object> set = setRedisService.sGet("searchHistory");
//        return new ResultInfo(set);
//    }

    //返回所有类别Back to category
    @PostMapping("/backToCategory")
    public ResultInfo backToCategory() {
        return null;
    }

    /**
     * 功能描述: 优惠券核销
     * @Param:
     * @return:
     * @Author: 况鑫威
     * @Date: 2021/7/6
     * @Time: 15:03
     */
    @PostMapping("/writeOff")
    public ResultInfo writeOff(String couponCode, HttpServletRequest request){
        CereShopOrder cereShopOrder = cereShopOrderService.selectBycouponCode(couponCode);
        if(cereShopOrder == null){
            return ResultInfo.error("未查到该券码,请检查填写是否正确");
        }
        if(cereShopOrder.getIsUse() != 1){
            return ResultInfo.error("该卡券已经核销,请勿再试");
        }
        String token = request.getHeader("Authorization");
        CereShopCouponRecording cereShopCouponRecording = cereShopOrderService.deleteByCouponCode(cereShopOrder.getOrderId(), token);
        return new ResultInfo<>(cereShopCouponRecording);
    }

    /**
     * 功能描述: 获取优惠券核销记录
     * @Param:
     * @return:
     * @Author: 况鑫威
     * @Date: 2021/7/7
     * @Time: 13:39
     */
    @PostMapping("/getOWritOff")
    public ResultInfo<?> getOWritOff(Map map){
        return new ResultInfo<>(cereShopOrderService.getOWritOff(map));
    }
}
